﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Xml.Linq;

namespace BaseTypesLib.Model.Backgrounds
{
    public enum GradientOrientation { Horizontal, Vertical }

    public class LinearGradientBackground:Background
    {
        private GradientOrientation orientation;
        public GradientOrientation Orientation
        {
            get { return orientation; }
            set
            {
                if (orientation != value)
                {
                    orientation = value;
                    OnPropertyChanged("Orientation");
                }
            }
        }

        private Color color1;
        public Color Color1
        {
            get { return color1; }
            set
            {
                if (color1 != value)
                {
                    color1 = value;
                    OnPropertyChanged("Color1");
                }
            }
        }
        private Color color2;
        public Color Color2
        {
            get { return color2; }
            set
            {
                if (color2 != value)
                {
                    color2 = value;
                    OnPropertyChanged("Color2");
                }
            }
        }
        public override string ToString()
        {
            return "Gradient";
        }
        public override XElement GetXml()
        {
            XElement elem = base.GetXml();
            elem.SetAttributeValue("Orientation", Orientation);
            elem.SetAttributeValue("Color1", Color1);
            elem.SetAttributeValue("Color2", Color2);
            return elem;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var o = elem.Attributes("Orientation").FirstOrDefault();
            if (o != null)
                Orientation = (GradientOrientation)Enum.Parse(typeof(GradientOrientation), o.Value);
            var c1 = elem.Attributes("Color1").FirstOrDefault();
            if (c1 != null)
                Color1 = (Color)ColorConverter.ConvertFromString(c1.Value);
            var c2 = elem.Attributes("Color2").FirstOrDefault();
            if (c2 != null)
                Color2 = (Color)ColorConverter.ConvertFromString(c2.Value);

        }
    }
}
